package com.junyidao.game.controller;

import com.junyidao.game.constant.BaseConstant;
import com.junyidao.game.constant.OperLogConstant;
import com.junyidao.game.core.LoginInfo;
import com.junyidao.game.core.annotation.Action;
import com.junyidao.game.pojo.SysPriv;
import com.junyidao.game.pojo.SysRole;
import com.junyidao.game.pojo.SysUser;
import com.junyidao.game.pojo.TChannel;
import com.junyidao.game.service.RoleService;
import com.junyidao.game.service.SysPrivService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zzh on 2016/8/27.
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private SysPrivService sysPrivService;

    @RequestMapping
    @Action(desc = "进入角色管理页面")
    public ModelAndView list() {
        ModelAndView result = new ModelAndView("system/role_list");
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/list")
    @Action(desc = "获取账号管理列表", sysPriv = false)
    public Map<String, Object> listData(SysRole role) {
        Map<String, Object> map = roleService.getAll(role);
        return resultSucess(map);
    }

    @ResponseBody
    @RequestMapping(value = "/activate")
    @Action(operType = OperLogConstant.ROLE_ACTIVATE, desc = "启用失效的角色", needLog = true)
    public Map<String, Object> activate(String roleIds) {
        roleService.activate(roleIds, BaseConstant.STATUS_NORMAL);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/disactivate")
    @Action(operType = OperLogConstant.ROLE_DISACTIVATE, desc = "注销启用的角色", needLog = true)
    public Map<String, Object> disactivate(String roleIds) {
        roleService.activate(roleIds, BaseConstant.STATUS_DELETE);
        return resultSucess(null);
    }

    @RequestMapping(value = "/add")
    @Action(desc = "进入新增角色页面")
    public ModelAndView add() {
        ModelAndView result = new ModelAndView("system/role_add");
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/privList")
    @Action(desc = "获取权限列表", sysPriv = false)
    public Map<String, Object> privList(Integer roleId) {
        Map<String, Object> map = new HashMap<>();
        List<SysPriv> list = sysPrivService.getSysPrivAll();
        if (roleId != null) {
            List<SysPriv> hasList = sysPrivService.getSysPrivIdListByRole(roleId);
            map.put("hasList", hasList);
        }
        map.put("list", list);
        return resultSucess(map);
    }

    @ResponseBody
    @RequestMapping(value = "/addRole")
    @Action(operType = OperLogConstant.USER_ADD, desc = "新增角色", needLog = true)
    public Map<String, Object> addRole(SysRole role, String privIds) {
        roleService.addRole(role, privIds);
        return resultSucess(null);
    }

    @ResponseBody
    @RequestMapping(value = "/update")
    @Action(operType = OperLogConstant.USER_EDIT, desc = "编辑角色", needLog = true)
    public Map<String, Object> update(SysRole sysRole, String privIds) {
        roleService.updateSysRole(sysRole, privIds);
        return resultSucess(null);
    }

    @RequestMapping(value = "/detail")
    @Action(desc = "账号详情")
    public ModelAndView detail(Integer roleId, String editFlag) {
        ModelAndView result = new ModelAndView("system/role_detail");
        SysRole role = roleService.selectByPrimaryKey(roleId);
        result.addObject("role", role);
        result.addObject("editFlag", editFlag);
        return result;
    }
}
